var parkFeeStatisJs = function (targetObj) {
	this.targetObj = targetObj;
	this.sysUtils = new kendoUtils();
	this.init();
	this.dateFormat = "yyyy-MM-dd HH:mm:ss";
	return this;
};

parkFeeStatisJs.prototype = {
		init : function () {
			var fieldArray = this.sysUtils.getBindFieldArray(this.targetObj.find('.search-panel'));
			var functionArray = [
			                     {fName : 'search_btn' , fs : this.findReport},
			                     {fName : 'reset_btn' , fs : this.resetInfo}
			                     ];
			this.models = new bindBaseModel(null, fieldArray, functionArray, null);
			this.sysUtils.bindDoucment(this.models, this.targetObj, this);
			this.initHtml();
			//默认查最近一个月的数据
			var date = new Date();
			date.setHours(8, 0, 0, 0);
			this.sysUtils.mvvmModel.set("endTime",kendo.toString(date,this.dateFormat));
			var bgTime = new Date();
			bgTime.setHours(8, 0, 0, 0);
			bgTime.setDate(bgTime.getDate() - 1);
			this.sysUtils.mvvmModel.set("beginTime",kendo.	toString(bgTime,this.dateFormat));
			Utils.createDataDictDropDownList({div:this.targetObj.find('#chargeMethod'), parentModelKey:BASIS.CHARGE_METHOD.ROOT}, this);
			//计算显示区域高度
			this.targetObj.find("#parkFeeReportDiv").height(this.targetObj.height() - this.targetObj.find(".search-panel").outerHeight());
		},
		/**
		 * 初始化界面元素
		 */
		initHtml:function(){
			this.userInfo = null;
			var that = this;
			var autoFlag = false;
			var selectFun = function(e){
				that.userInfo = this.dataItem(e.item);
				autoFlag = true;
			};
			var changeFun = function(e){
				if(!autoFlag){
					that.userInfo = null;
					that.targetObj.find("#cashierId").val("");
				}
				autoFlag = false;
			};
			this.sysUtils.autoCompleteUser("cashierId", "userName",selectFun,changeFun);
			/**
			 * 获取所有工作站节点树
			 */
			JsonpAjax(baseApiPath + 'layout/wsn/get', '', 'json', function(res) {
				var potionTree = that.targetObj.find("#parkFee-tree").lonixFloat({
					source: res,
					setting: {
						view: {
							showLine: true,
							selectedMulti: false,
							dblClickExpand: false,
							showIcon: false
						},
						data : {
							simpleData: {
								enable: true,
								idKey: "orgCode",
								pIdKey: "parentCode",
								rootPId:""
							}
						}
					},
					searchField: 'name',
					target: that.targetObj.find("#orgName"),
					primary: function (nodes) {
						if(nodes.length <= 0 ) return;
						that.targetObj.find('#parkFee-tree').fadeUp();
						that.parkOrgCode = nodes[0]["orgCode"];
						//显示层级
						that.targetObj.find("#orgName").val(nodes[0]["name"]+"("+that.parkOrgCode+")");
					}
				});
			}, false);
		},
		/*
		 * 重置按钮
		 */
		resetInfo:function(e){
			var that = $(e.target).data("that");
			that.sysUtils.setBindFieldNull(that.models.bindFieldArray);
			that.userInfo = null;
			that.targetObj.find("#cashierId").val("");
			that.parkOrgCode = null;
			that.targetObj.find("#orgName").val("");
			//默认查最近一个月的数据
			var date = new Date();
			that.sysUtils.mvvmModel.set("endTime",kendo.toString(date,that.dateFormat));
			date.setMonth(date.getMonth() - 1);
			that.sysUtils.mvvmModel.set("beginTime",kendo.toString(date,that.dateFormat));
		},
		/**
		 * 获取查询参数并校验
		 * @param params
		 */
		getQueryParams:function(params){
			if(isEmpty(params["beginTime"]) || isEmpty(params["endTime"])){
				notification({title : '提示', message : '开始时间和结束时间为必填，请选择！！'}, 'info');
				return;
			}
			params["beginTime"] = kendo.toString(params["beginTime"],this.dateFormat);
			params["endTime"] = kendo.toString(params["endTime"],this.dateFormat);
			//检查查询时间段是否超过半年
			var beginTime = kendo.parseDate(params["beginTime"],this.dateFormat);
			var endTime =  kendo.parseDate(params["endTime"],this.dateFormat);
			beginTime.setMonth(beginTime.getMonth() + 6);
			if(endTime > beginTime){
				notification({title : '提示', message : '开始时间和结束时间相差不能超过半年！！'}, 'info');
				return;
			}
			if(null != this.userInfo && !isEmpty(this.targetObj.find("#cashierId").val())){
				params["cashierId"] =  this.userInfo.id;
			}
			if(null != this.parkOrgCode){
				params["parkOrgCode"] =  this.parkOrgCode;
			}
			return params;
		},
		/**
		 * 查询报表数据
		 * @param e
		 */
		findReport:function(e){
			var that = this;
			if(null != e){
				that = $(e.target).data("that");
			}
			that.targetObj.find("#reportTable").find("tr:not(:first)").empty();
			that.targetObj.find("#timeReport").html("");
			that.targetObj.find("#clearingTime").html("");
			that.targetObj.find("#totalMoney").html("");
			var params = that.sysUtils.getBindDocJson(that.models.bindFieldArray);
			params = that.getQueryParams(params);
			JsonpAjax(baseApiPath + 'report/workstationQuery', JSON.stringify(params), 'json', function (maps) {
				var res = maps["list"];
				var jsonObject = maps["object"];
				if(isEmpty(res)){
					notification({title : '提示', message : '此时间段无任何统计数据！！'}, 'info');
					return;
				}
				var tr = null;
				for(var i = 0 ; i <res.length;i++){
					var nbsp = "";
					tr = $("<tr></tr>");
					for(var j = 0; j<res[i]["level"];j++){
						nbsp +="--"
					}
					if("park" == res[i]["orgType"]){
						tr.attr("class","tableColor01");
					}else if("par" == res[i]["orgType"]){
						tr.attr("class","tableColor02");
					}else if("wsn" == res[i]["orgType"]){
						tr.attr("class","tableColor03");
					}else if("chn_out" == res[i]["orgType"]){
						tr.attr("class","tableColor04");
					}else if("US" == res[i]["orgType"]){
						tr.attr("class","tableColor05");
					}else if("CM" == res[i]["orgType"]){
						tr.attr("class","tableColor06");
					}
					if("CM" != res[i]["orgType"] && "US" != res[i]["orgType"]){
						var td = $("<td style='font-weight:bold;' rowspan=1>"+"<span class='table-line01'>"+"</span>"+(isNotEmpt(res[i]["orgName"])?nbsp+res[i]["orgName"]:"")+"</td>");
						td.attr("orgCode",res[i]["orgCode"]);
						tr.append(td);
					}
					if("CM" != res[i]["orgType"]){	
						var toTr = $("<td>"+(isNotEmpt(res[i]["chargeUserName"])?res[i]["chargeUserName"]:"")+"</td>");
						if("US" ==  res[i]["orgType"]){
							toTr.attr("rowspan",res[i]["mergeRow"]);
							var parentTd = that.targetObj.find("td[orgCode='"+res[i]["parentCode"]+"']");
							var oldRowspan = parentTd.attr("rowspan");
							parentTd.attr("rowspan",res[i]["mergeRow"] + (isEmpty(oldRowspan)?0:parseInt(oldRowspan)));
						}
						tr.append(toTr);
					}
					tr.append($("<td>"+(isNotEmpt(res[i]["chargeMethodName"])?res[i]["chargeMethodName"]:"")+"</td>"));
					tr.append($("<td>"+(isNotEmpt(res[i]["chargeMoney"])?res[i]["chargeMoney"].toFixed(2):"")+"</td>"));
					tr.append($("<td>"+(isNotEmpt(res[i]["dueMoney"])?res[i]["dueMoney"].toFixed(2):"")+"</td>"));
					tr.append($("<td>"+(isNotEmpt(res[i]["preMoney"])?res[i]["preMoney"].toFixed(2):"")+"</td>"));
					that.targetObj.find("#reportTable").append(tr);
				}
				//改变title信息
				that.targetObj.find("#timeReport").html(params["beginTime"] +" - " +params["endTime"] + " ");
//				that.targetObj.find("#clearingTime").html(jsonObject["clearingTime"]);
				that.targetObj.find("#totalMoney").html(jsonObject["totalMoney"].toFixed(2));
			}, true);
		},
		/**
		 * 打印
		 * @param e
		 */
		print:function(e){
			this.targetObj.find("#parkFeeReportDiv").find("div:first").printArea({
				mode:"popup",
				popTitle:"",
				popClose:true,
				popHt:600,
				popWd:1000
			});
		},
		/**
		 * 导出Excel文件
		 */
		exportExcel:function(){
			var params = this.sysUtils.getBindDocJson(this.models.bindFieldArray);
			params = this.getQueryParams(params);
			window.open(baseApiPath+'report/exportWorkExcel?jsonData='+encodeURI(JSON.stringify(params)));
		}

};